package com.example.secondhandshop.model;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.bean.ResponseBody;
import com.example.secondhandshop.ui.activity.Constans;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NewMassageModel {
    OkHttpClient client = new OkHttpClient();
    public void NewMassageModel(int fromuserid,int userid,  String content,NewMassageModel.OnProductsFetchedListener listener) {
        new Thread(() -> {
            // url路径
            String url = Constans.SERVER_URL() +"tran/chat";
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("appId", Constans.getAppId())
                    .add("appSecret", Constans.getAppSecret())
                    .add("Accept", "application/json, text/plain, */*")
                    .build();
            // 请求体
            // PS.用户也可以选择自定义一个实体类，然后使用类似fastjson的工具获取json串
            Map<String, Object> bodyMap = new HashMap<>();
            bodyMap.put("content", content);
            bodyMap.put("toUserId", fromuserid);
            bodyMap.put("userId", userid);
            // 将Map转换为字符串类型加入请求体中
            Gson gson=new Gson();
            String body = gson.toJson(bodyMap);
            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, body))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Type jsonType = new TypeToken<ResponseBody<Object>>(){}.getType();
                        // 获取响应体的json串
                        String body = response.body().string();
                        Log.d("info", body);
                        // 解析json串到自己封装的状态
                        ResponseBody<Object> dataResponseBody = gson.fromJson(body,jsonType);
                        if (dataResponseBody.getCode()==200){
                            listener.onSuccess();
                        }
                        else {
                            listener.onFailure();
                        }
                        Log.d("info", dataResponseBody.toString());
                    }
                });
            } catch (NetworkOnMainThreadException ex) {
                ex.printStackTrace();
            }
        }).start();
    }

    public interface OnProductsFetchedListener {
        void onSuccess();
        void onFailure();
    }
}
